package com.zhuangwei.rest;

import com.alibaba.fastjson.JSONObject;
import com.zhuangwei.config.BizConfig;
import com.zhuangwei.config.ValueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 文希
 * @create 2019-05-27 11:19
 **/
@RestController
@Slf4j
public class DemoController {
    @Autowired
    private ContextRefresher contextRefresher;
    @Autowired
    private BizConfig bizConfig;
    @Autowired
    private ValueConfig valueConfig;

    @Value("${rest.uuid}")
    private String uuid;

    @GetMapping("/show")
    public String show(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("biz",JSONObject.toJSONString(bizConfig));
        jsonObject.put("uuid",valueConfig.getUuid());
        jsonObject.put("no-refresh",uuid);
        return jsonObject.toJSONString();
    }

    @GetMapping(path = "/refresh")
    public String refresh(){
        new Thread(()->contextRefresher.refresh()).start();
        return show();
    }

    //  this.context.publishEvent(new RefreshScopeRefreshedEvent());
    @EventListener
    public void envListener(EnvironmentChangeEvent event){
      log.info("conf change:{}",event);
    }
}
